package nsga.chromosome;

import java.util.ArrayList;
import java.util.List;

import abstractGA.GAParameter;
import abstractGA.chrom.Chrom;
import algorithm.AlgCtx;
import algorithm.SAElem;
import rulSeqGA.chromosome.RulSeqGAChrom;

/**
 * 多目标不带学习机制的规则序列染色体
 * @author ls
 *
 */
public class NSRulSeqGAChrom extends RulSeqGAChrom {
	
	public NSRulSeqGAChrom() {
		super();
	}

	public NSRulSeqGAChrom(List<StringBuffer> codes) {
		this.codes = codes;
	}
	
	/**
     * 构造函数
     * @param algCtx：算法运行上下文
     * @param parameter：参数
     */
	public NSRulSeqGAChrom(AlgCtx algCtx,GAParameter parameter) {
		super(algCtx,parameter);
	}
	
	/**
	 * @return:复制并返回一条新的染色体
	 */
	@Override
	public Chrom copy() {
		// 产生规则号序列的NSRulSeqGA染色体
		NSRulSeqGAChrom newChrom = new NSRulSeqGAChrom();
		List<StringBuffer> newCodes = new ArrayList<StringBuffer>();
		int len = codes.size();
		for (int i = 0; i < len; i++) {
			String tempStr = codes.get(i).toString();
			newCodes.add(new StringBuffer(tempStr));
		}
		newChrom.codes = newCodes;// 这里的newCodes与codes内容相同但数组的每元素都是不同指针

		// 复制各位上的自由度实例列表
		ArrayList<ArrayList<StringBuffer>> cpyDfCodesList = new ArrayList<ArrayList<StringBuffer>>();
		for (ArrayList<StringBuffer> dfCodes : dfCodesList) {
			ArrayList<StringBuffer> cpyDfCodes = new ArrayList<StringBuffer>();
			for (StringBuffer code : dfCodes) {
				cpyDfCodes.add(new StringBuffer(code));
			}
			cpyDfCodesList.add(cpyDfCodes);
		}
		newChrom.dfCodesList = cpyDfCodesList;

		// 复制各位上的变更记录列表
		ArrayList<ArrayList<SAElem>> cpyChgElems = new ArrayList<ArrayList<SAElem>>();
		for (ArrayList<SAElem> cpyElems : chgElemsList) {
			ArrayList<SAElem> cpyChgElem1 = new ArrayList<SAElem>();
			for (SAElem elem : cpyElems) {
				cpyChgElem1.add(elem);
			}
			cpyChgElems.add(cpyChgElem1);
		}
		newChrom.chgElemsList = cpyChgElems;

		newChrom.inValidPos = inValidPos;
		newChrom.ruleAlgOpt = ruleAlgOpt;

		ArrayList<Float> newObjVals = new ArrayList<Float>();
		for(Float objVal:objVals){
			newObjVals.add(objVal);
		}
		newChrom.objVals = newObjVals;
		

		newChrom.para = para;
		newChrom.random = random;
		newChrom.algOper = algOper;
		newChrom.listener = newChrom;
		return newChrom;
	}

}
